/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package sim_test;
import java.awt.event.MouseEvent;
import javax.swing.*;
import java.awt.*;
import java.awt.GridLayout;
import java.awt.Dimension;
import java.awt.event.MouseListener;

/**
 *
 * @author Administrator
 */
public class Sim_JPanelGraphics extends JPanel {
    
    private Sim_ControllerMain simControllerMain;
    private int[] numberOfSquaresInGridLayout;
    private GridLayout gridLayout;
    private JLabel jLabelDebug;
    private JButton jButtonDebug;
    private JButton jButtonDebug2;
    private JLabel jLabelCar1;
    private Sim_Gfx_Car car1Gfx;
    private int sizeOfTiles = 75; // change later
    private Sim_Map map;
    
    public Sim_JPanelGraphics(Sim_ControllerMain scm, Dimension size)
    {
        car1Gfx = new Sim_Gfx_Car();
        simControllerMain = scm;
        map = simControllerMain.getSimLoader().getMap1();
        
        setOpaque(false);
        setPreferredSize(size);
        
        createLayout();
        addContent();
        addListeners();
        
        
    }
    
    private void addContent()
    {
        //jLabelDebug = new JLabel("debug label 1");
        //add(jLabelDebug);
        jLabelCar1 = new JLabel(car1Gfx.getCurrentImageIcon());
        //jButtonDebug.setPreferredSize(new Dimension(25,25));
        //jButtonDebug2 = new JButton("debug button 2");
        //jButtonDebug2.setPreferredSize(new Dimension(25,25));
        //add(jButtonDebug);
        //add(jButtonDebug2);
        jLabelCar1.setBounds(0, 0, 75, 75);
        add(jLabelCar1);
        
    }
    
    private void addListeners()
    {
        jLabelCar1.addMouseListener(new MouseListener() {

            public void mouseClicked(MouseEvent e) {
                //int x = e.getX();
                //int y = e.getY();
                int destX = 4;
                int destY = 1;
                
                System.out.println("clicked");
                System.out.println("direction for: " + destX + "," + destY +  " currentPos: + " + car1Gfx.getPosInGridX() + "," + car1Gfx.getPosInGridY() + " " + map.getDirecForDest(car1Gfx.getPosInGridX(), car1Gfx.getPosInGridY(), 14, 14));
                updateCar1Graphics(map.getDirecForDest(car1Gfx.getPosInGridX(), car1Gfx.getPosInGridY(), destX, destY));
                updateCar1Position();
                
            }

            public void mousePressed(MouseEvent e) {
                
            }

            public void mouseReleased(MouseEvent e) {
              
            }

            public void mouseEntered(MouseEvent e) {
                
            }

            public void mouseExited(MouseEvent e) {
                
            }
        });
    }
    
    private void updateCar1Position()
    {
        jLabelCar1.setBounds(car1Gfx.getPosX(), car1Gfx.getPosY(), sizeOfTiles, sizeOfTiles);
    }
    
    private void createLayout()
    {
        numberOfSquaresInGridLayout = simControllerMain.getSimEngine().getNumberOfSquares();
        //gridLayout = new GridLayout(numberOfSquaresInGridLayout[0], numberOfSquaresInGridLayout[1]);
        //setLayout(gridLayout);
        setLayout(null);
        
    }
    
    public void updateCar1Graphics(byte direction)
    {
        if(direction == 1)
        {
            car1Gfx.movedNorth();
            jLabelCar1.setIcon(car1Gfx.getCurrentImageIcon());
        }
        
        else if(direction == 2)
        {
            car1Gfx.movedEast();
            jLabelCar1.setIcon(car1Gfx.getCurrentImageIcon());
        }
        
        else if(direction == 4)
        {
            car1Gfx.movedSouth();
            jLabelCar1.setIcon(car1Gfx.getCurrentImageIcon());
        }
        
        else if(direction == 8)
        {
            car1Gfx.movedWest();
            jLabelCar1.setIcon(car1Gfx.getCurrentImageIcon());
        }
        
        else
        {
            assert false : "direction is not valid for updating car gfx" + direction;
        }
    }
    

    
    @Override
    public void paintComponent(Graphics g)
    {
        //g.drawImage(car1Gfx.getCurrentImage(), 0 , 0, null);        
    }

      


}
